﻿####################################################################
# 定义目标路径和文件名
$targetPath = "D:\Program Files\Siemens\NX 10.0\UGII\menus"
$ugStartPath = "D:\OneDrive - lumao\0\电子书\机械设计大典.pdf"  
$targetFileName = "custom_dirs.dat"
###################################################################

# 加载Windows窗体程序集
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# 创建主窗体（需要全局变量以便在函数中访问）
$form = New-Object System.Windows.Forms.Form
$form.Text = "UG配置启动器"
$form.Size = New-Object System.Drawing.Size(300, 150)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false

# 执行配置1的逻辑
function Execute-Configuration1 {
    # 获取当前脚本所在目录
    $scriptDir = $PSScriptRoot
    
    # 配置1文件路径
    $sourceFile = Join-Path -Path $scriptDir -ChildPath "custom_dirs-1.dat"
    
    if (-not (Test-Path -Path $sourceFile -PathType Leaf)) {
        [System.Windows.Forms.MessageBox]::Show("错误：配置1文件不存在 - $sourceFile", "文件错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
        return
    }
    
    try {
        # 检查目标路径是否存在
        if (-not (Test-Path -Path $targetPath -PathType Container)) {
            [System.Windows.Forms.MessageBox]::Show("错误：目标路径不存在 - $targetPath", "路径错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
            return
        }
        
        # 定义标记文件路径
        $marker1Path = Join-Path -Path $targetPath -ChildPath "配置1"
        $marker2Path = Join-Path -Path $targetPath -ChildPath "配置2"
        
        # 清理旧标记
        if (Test-Path -Path $marker1Path) { Remove-Item -Path $marker1Path -Force }
        if (Test-Path -Path $marker2Path) { Remove-Item -Path $marker2Path -Force }
        
        # 复制配置文件
        Copy-Item -Path $sourceFile -Destination (Join-Path $targetPath $targetFileName) -Force
        
        # 创建新标记
        New-Item -Path $marker1Path -ItemType File -Force | Out-Null
        
        # 打开指定路径的程序/文件
        if (Test-Path -Path $ugStartPath) {
            Write-Host "正在打开程序：$ugStartPath" -ForegroundColor Cyan
            Start-Process -FilePath $ugStartPath
            # 操作成功后关闭窗口
            $script:form.Close()
        }
        else {
            [System.Windows.Forms.MessageBox]::Show("警告：指定的程序路径不存在 - $ugStartPath", "路径警告", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
        }
    }
    catch {
        [System.Windows.Forms.MessageBox]::Show("操作失败：$_", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
}

# 执行配置2的逻辑
function Execute-Configuration2 {
    # 获取当前脚本所在目录
    $scriptDir = $PSScriptRoot
    
    # 配置2文件路径
    $sourceFile = Join-Path -Path $scriptDir -ChildPath "custom_dirs-2.dat"
    
    if (-not (Test-Path -Path $sourceFile -PathType Leaf)) {
        [System.Windows.Forms.MessageBox]::Show("错误：配置2文件不存在 - $sourceFile", "文件错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
        return
    }
    
    try {
        # 检查目标路径是否存在
        if (-not (Test-Path -Path $targetPath -PathType Container)) {
            [System.Windows.Forms.MessageBox]::Show("错误：目标路径不存在 - $targetPath", "路径错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
            return
        }
        
        # 定义标记文件路径
        $marker1Path = Join-Path -Path $targetPath -ChildPath "配置1"
        $marker2Path = Join-Path -Path $targetPath -ChildPath "配置2"
        
        # 清理旧标记
        if (Test-Path -Path $marker1Path) { Remove-Item -Path $marker1Path -Force }
        if (Test-Path -Path $marker2Path) { Remove-Item -Path $marker2Path -Force }
        
        # 复制配置文件
        Copy-Item -Path $sourceFile -Destination (Join-Path $targetPath $targetFileName) -Force
        
        # 创建新标记
        New-Item -Path $marker2Path -ItemType File -Force | Out-Null
        
        # 打开指定路径的程序/文件
        if (Test-Path -Path $ugStartPath) {
            Write-Host "正在打开程序：$ugStartPath" -ForegroundColor Cyan
            Start-Process -FilePath $ugStartPath
            # 操作成功后关闭窗口
            $script:form.Close()
        }
        else {
            [System.Windows.Forms.MessageBox]::Show("警告：指定的程序路径不存在 - $ugStartPath", "路径警告", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
        }
    }
    catch {
        [System.Windows.Forms.MessageBox]::Show("操作失败：$_", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
}

# 创建按钮1 - 配置1
$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(50, 30)
$button1.Size = New-Object System.Drawing.Size(80, 30)
$button1.Text = "配置 1"
$button1.Add_Click({ Execute-Configuration1 })

# 创建按钮2 - 配置2
$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Point(170, 30)
$button2.Size = New-Object System.Drawing.Size(80, 30)
$button2.Text = "配置 2"
$button2.Add_Click({ Execute-Configuration2 })

# 添加说明标签
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20, 80)
$label.Size = New-Object System.Drawing.Size(260, 20)
$label.Text = "请选择要使用的UG配置"
$label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter

# 将控件添加到窗体
$form.Controls.Add($button1)
$form.Controls.Add($button2)
$form.Controls.Add($label)

# 显示窗体
$form.ShowDialog() | Out-Null

Pause